package com.mytransaction.jdbcpackage.imple;
import  com.mytransaction.jdbcpackage.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

@Component
@Scope("provided")
public  class  MyTransation implements MyTransationInter {
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    // TransactionStatus  会引起线程不安全的问题
    //private  TransactionStatus transactionStatus;
    @Override
    public TransactionStatus begin() {
        return dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
    }

    @Override
    public  void commit(TransactionStatus transactionStatus) {
        dataSourceTransactionManager.commit(transactionStatus);
    }

    @Override
    public void rollBack(TransactionStatus transactionStatus) {
        dataSourceTransactionManager.rollback(transactionStatus);
    }
}
